package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.PersonResidentVoMapper;
import com.yyds.mapper.PersonVolunteerMapper;
import com.yyds.pojo.PersonVolunteer;
import com.yyds.service.PersonVolunteerService;
import com.yyds.vo.EchartBarVo;
import com.yyds.vo.PersonVolunteerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 花君瑜
 * @date 2022/10/14 18:43:40
 * @desc
 **/
@Service("/personVolunteerService")
public class PersonVolunteerServiceImpl implements PersonVolunteerService {
    @Autowired
    private PersonVolunteerMapper personVolunteerMapper;
    @Autowired
    private PersonResidentVoMapper personResidentVoMapper;


    @Override
    public MyJSON<PersonVolunteerVo> queryAllPersonVolunteerService(Map<String, Object> params) {
        //mybatis分页 插件的使用
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);
        List<PersonVolunteerVo> personVolunteers = personVolunteerMapper.queryAllPersonVolunteerMapper(params);
        System.out.println("List<PersonVolunteer> personResidents ="+personVolunteers);
        //mybatis自带的分页数据结构
        PageInfo<PersonVolunteerVo> personVolunteerPageInfo = new PageInfo<>(personVolunteers);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        MyJSON<PersonVolunteerVo> personVolunteerMyJSON = new MyJSON<>();
        personVolunteerMyJSON.setCode(0);
        personVolunteerMyJSON.setMsg("居民信息分页列表数据");
        personVolunteerMyJSON.setCount(personVolunteerPageInfo.getTotal());
        personVolunteerMyJSON.setData(personVolunteerPageInfo.getList());
        System.out.println(personVolunteerMyJSON);
        return personVolunteerMyJSON;
    }

    @Override
    public Integer deletePersonVolunteerService(Integer Volunteer_id) {
        return personVolunteerMapper.deletePersonVolunteerMapper(Volunteer_id);
    }

    @Override
    public Integer deleteAllPersonVolunteerService(String str) {
        //1-2-3
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return personVolunteerMapper.deleteAllPersonVolunteerMapper(list);
    }

    @Override
    public Integer addPersonVolunteerService(PersonVolunteerVo personVolunteerVo) {
        PersonVolunteer personVolunteer = new PersonVolunteer();
        //根据地区名 查 地区id
        Integer region_id = personVolunteerMapper.queryRegionIdMapper(personVolunteerVo.getRegion_name());
        //根据地区id 查 封控地区id
        Integer area_id = personVolunteerMapper.queryAreaIdMapper(region_id);
        personVolunteer.setArea_id(area_id);
        //根据居民名称 查 居民id
        Integer resident_id = personResidentVoMapper.queryVaccineIdMapper(personVolunteerVo.getResident_name());
        personVolunteer.setResident_id(resident_id);
        return personVolunteerMapper.addPersonVolunteerMapper(personVolunteer);
    }

    @Override
    public Integer updatePersonVolunteerService(PersonVolunteerVo personVolunteerVo,Integer Volunteer_id) {
        PersonVolunteer personVolunteer = new PersonVolunteer();
        //根据地区名 查 地区id
        Integer region_id = personVolunteerMapper.queryRegionIdMapper(personVolunteerVo.getRegion_name());
        //根据地区id 查 封控地区id
        Integer area_id = personVolunteerMapper.queryAreaIdMapper(region_id);
        personVolunteer.setArea_id(area_id);
        //根据居民名称 查 居民id
        Integer resident_id = personResidentVoMapper.queryVaccineIdMapper(personVolunteerVo.getResident_name());
        personVolunteer.setResident_id(resident_id);
        personVolunteer.setVolunteer_id(Volunteer_id);
        return personVolunteerMapper.updatePersonVolunteerMapper(personVolunteer);
    }

    @Override
    public List<String> queryAllRegionNameService() {
        return personVolunteerMapper.queryAllRegionNameMapper();
    }

    @Override
    public List<EchartBarVo> queryPersonVolunteerService() {
        return personVolunteerMapper.queryPersonVolunteerMapper();
    }
}
